/*
 * Copyright 2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.dopdf.document;

import org.dopdf.document.hash.strategy.DocumentHashingStrategy;
import org.dopdf.document.hash.strategy.LRUBasedDocumentHashingStrategy;
import org.dopdf.document.read.pdf.PDFDocumentReader;

import java.io.InputStream;

/**
 * The class that represents a PDF document.  This document implements the {@link org.dopdf.document.Document}
 * interface.  This class is responsible to handel pdf documents.
 *
 * @author Deep Shah
 */
public class PDFDocument implements Document {

    /**
     * The id of the document.
     */
    private String id;

    /**
     * The number of pages reference.
     */
    private int numberOfPages;

    /**
     * The constructor that takes in the InputStream representing the pdf coument and pdfFetching strategy as argument.
     * This method calls the init method after converting the input stream into byte array.
     * @param inputStream the input stream reference representing the pdf document.
     * @param documentHashingStrategy the pdf fetching strategy which determins where the pdf will be stored.
     */
    public PDFDocument(final InputStream inputStream, final DocumentHashingStrategy documentHashingStrategy) {
        init(new PDFDocumentReader(inputStream), documentHashingStrategy);
    }

    /**
     * The constructor that takes in only pdf input stream as argument.  The default strategy is used to save the pdf.
     * @param inputStream the stream that represents the pdf document.
     */
    public PDFDocument(final InputStream inputStream) {
        this(inputStream, LRUBasedDocumentHashingStrategy.getInstance());
    }

    /**
     * The method that does the initialization of various properties.
     * @param pdfDocumentReader the reader for the pdf document.
     * @param documentHashingStrategy the pdf hashing strategy used to hash the document.
     */
    private void init(final PDFDocumentReader pdfDocumentReader,
                      final DocumentHashingStrategy documentHashingStrategy) {
        id = documentHashingStrategy.hash(pdfDocumentReader);
        numberOfPages = pdfDocumentReader.getNumberOfPages();
    }

    /**
     * {@inheritDoc}
     */
    public int getNumberOfPages() {
        return numberOfPages;
    }

    /**
     * {@inheritDoc}
     */
    public String getId() {
        return id;
    }
}
